package com.google.code.jmarias.server.phases;

import com.google.code.jmarias.common.Card;
import com.google.code.jmarias.common.action.PickCardFromPeopleAction;
import com.google.code.jmarias.common.action.SignedAction;
import com.google.code.jmarias.common.NewPlayer;
import com.google.code.jmarias.common.NewTable;
import com.google.code.jmarias.common.action.UnhideFiveCardsAction;
import com.google.code.jmarias.server.tcp.MariasGame;

/**
 *
 * @author josef
 */
public class ChooseFromPeoplePhase extends NewAbstractPhase {

    public ChooseFromPeoplePhase(NewPlayer playingNow) {
        super(playingNow);
        addListener(new ActionListener(PickCardFromPeopleAction.class) {

            @Override
            protected NewAbstractPhase processAction(SignedAction action, NewTable table) {

                PickCardFromPeopleAction a = (PickCardFromPeopleAction) action.getRawAction();
                a.setPickedCard(table.getPlayer(MariasGame.forehand).getHand().get(7 + a.getCardOrder()));
                broadcast(action, table.getPlayers());

                Card[] cards = table.getPlayer(MariasGame.forehand).getHand().subList(7, 12).toArray(new Card[]{});
                SignedAction action2 = new SignedAction(new UnhideFiveCardsAction(cards, table.getPlayer(MariasGame.forehand).getClient()));
                broadcast(action2, table.getPlayer(MariasGame.forehand));

                return new TalonDroppingPhase(table.getPlayer(MariasGame.forehand));
            }
        });
    }

    @Override
    public String toString() {
        return "Volba z lidu";
    }

//    public AbstractPhase processAction(GameAction action) {
//        if (isSameAction(PickCardFromPeopleAction.class, action)) {
//            return processPickCardFromPeopleAction((PickCardFromPeopleAction) action);
//        }
//        return null;
//    }
//
//    private AbstractPhase processPickCardFromPeopleAction(PickCardFromPeopleAction action) {
//        Card[] hiddenCards = table.getFiveCards(2);
//        Card pickedCard = hiddenCards[action.getCardOrder()];
//        table.getClients().get(0).sendMessage(new PickCardFromPeopleAction(pickedCard));
//        table.getClients().get(0).sendMessage(new DealFiveCardsAction(hiddenCards));
//        table.setTrumpSuit(pickedCard.getSuit());
//        return new TalonDroppingPhase();
//    }
//
}
